package znbkTestPaperNtxl.savePaper;

import android.app.Activity;
import android.content.DialogInterface;
import android.os.Handler;
import android.util.Log;

import znbkListXtfx.ISystemReviewPresenter;
import com.example.lancooznbk.view.base.ILoadPaperView;
import com.lancoo.reviewsystem.utils.ToastUtils;
import com.lancoo.rxretrofitlibrary.Api.UserInfo;
import com.lancoo.rxretrofitlibrary.http.HttpManager;
import com.lancoo.rxretrofitlibrary.listener.HttpOnNextListener;

import znbkTestPaperNtxl.ZnbkPaperNtxlInfor;
import znbkTestPaperNtxl.activity.TrainingDifficultProblemsPaperActivity;


/**
 * Created by Administrator on 2018\2\28 0028.
 */

public class SaveTrainingDifficultProblemsPresenter implements ISystemReviewPresenter {

    private ILoadPaperView view;
    private int ModelFrom;
    private boolean isFromExit = false;
    private boolean isAssess = false;

    public SaveTrainingDifficultProblemsPresenter(ILoadPaperView view, int modelFrom) {
        this.view = (ILoadPaperView) view;
        this.ModelFrom = modelFrom;
    }

    @Override
    public void getData(boolean isShowLoading) {
        //  if (isShowLoading) view.showLoading();
        HttpOnNextListener<String> httpOnNextListener = new HttpOnNextListener<String>() {

            @Override
            public void onNext(String s) {

                if(s.equals("1")){

                    if (isFromExit){
                        ToastUtils.showShort("提交成功");
                        Log.e("==isFromExit==","======"+isFromExit);
                            view.getLoadingDialog().setOnDismissListener(new DialogInterface.OnDismissListener() {
                                @Override
                                public void onDismiss(DialogInterface dialog) {
                                    if(isAssess){
                                        view.openAssessDialog();
                                    }else {
                                        view.openExamReportDialog();
                                    }
                                }
                            });
                             view.updatingResult(true);

                    }else{
                        ToastUtils.showShort("保存成功");
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                ((Activity)view).finish();
                            }
                        },600);

                    }
                }else{
                    view.updatingResult(false);
                    ToastUtils.showShort("提交失败! 错误码"+s.toString());
                }

            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                //view.showNetError();
                ToastUtils.showShort("提交失败!");
            }
        };

        SaveTrainingDifficultProblemsApi api = new SaveTrainingDifficultProblemsApi(httpOnNextListener, (TrainingDifficultProblemsPaperActivity)view);

//        api.setSaveTrainingDifficultProblemsJson(SaveTrainingDifficultProblemsModel.getSaveTrainingDifficultProblemsJson(
//                ZnbkPaperNtxlInfor.QuesID, UserInfo.StuId,UserInfo.StuName,UserInfo.SubjectId,UserInfo.StageNo,
//                ZnbkPaperNtxlInfor.ItemFrom,ZnbkPaperNtxlInfor.TypeID,ZnbkPaperNtxlInfor.TypeName,ZnbkPaperNtxlInfor.QuesBrief,ZnbkPaperNtxlInfor.ListStuAnswer,
//                ZnbkPaperNtxlInfor.CorrectRate,ZnbkPaperNtxlInfor.TotalItemCount, ZnbkPaperNtxlInfor.AnswerItemCount,ZnbkPaperNtxlInfor.Duration,
//                ZnbkPaperNtxlInfor.AnswerState));

        HttpManager httpManager = HttpManager.getInstance();
        httpManager.doHttpDeal(api);
    }

    @Override
    public void getMoreData() {
        getData(false);
    }

    @Override
    public void getDataByStatus(int position) {
        getData(false);
    }

    public void getData(boolean isShowing ,boolean isFromExit,boolean isAssess){
        this.isFromExit = isFromExit;
        this.isAssess = isAssess;
        getData(isShowing);
    }

}